//
//  JWaitPayView.m
//  HeadIndustry
//
//  Created by jun on 2017/9/8.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JWaitPayView.h"
#import <UITableView+FDTemplateLayoutCell.h>
#import "JWiatPayCell.h"
#import "JWaitReceptionCell.h"
#import "JReturnProductCell.h"
#import "JAccomplishOrderCell.h"
#import "JWaitCommentCell.h"

#import "JGroupOrderCell.h" // 团购待付款
#import "JGroupWaitReceptionCell.h" // 团购待发货
#import "JGroupCommentCell.h" // 团购待评价
#import "JGroupReturnCell.h"
#import "JGroupAccomplishCell.h"



@interface JWaitPayView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation JWaitPayView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        _dataArr = [NSArray array];
        _dataGroupArr = [NSArray array];
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 200;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    [_tableView registerClass:[JWiatPayCell class] forCellReuseIdentifier:NSStringFromClass([JWiatPayCell class])];
//    [_tableView registerClass:[JWaitReceptionCell class] forCellReuseIdentifier:NSStringFromClass([JWaitReceptionCell class])];
    
//    [_tableView registerClass:[JReturnProductCell class] forCellReuseIdentifier:NSStringFromClass([JReturnProductCell class])];

    
    [self addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_dataGroupArr.count == 0) {
        return 1;
    }
    else
    {
        return 2;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataGroupArr.count == 0) {
        return _dataArr.count;
    }
    else
    {
        if (section == 0) {
            return _dataArr.count;
        }
        else
        {
            return _dataGroupArr.count;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_selOrderType == 0) {
                
       
            if (indexPath.section == 0) {
                JWiatPayCell *cell = [[JWiatPayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JWiatPayCell class])];
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.waitPayModel = _dataArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num, JWaitPayModel *waitPayModel) {
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    if (num == 1) { // 付款
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate OrderPayMethod:waitPayModel];
                        };
                    }
                    else if (num == 2) { // 取消订单
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate cancleOrderMethod:waitPayModel];
                        };
                    }
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;

            }
            else
            {
                JGroupOrderCell *cell = [[JGroupOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JGroupOrderCell class])];
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.groupModel = _dataGroupArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num,JOrderGroupModel *waitPayModel) {
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    if (num == 1) { // 付款
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate OrderPayMethod:waitPayModel];
                        };
                    }
                    else if (num == 2) {  // 取消订单
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate cancleOrderMethod:waitPayModel];
                        };
                    }
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
            }
        
    }
    
    if (_selOrderType == 1) {
        
        
            if (indexPath.section == 0) {
                JWaitReceptionCell *cell = [[JWaitReceptionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JWaitReceptionCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.waitPayModel = _dataArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num, JWaitPayModel *waitPayModel) {
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    if (num == 1) { // 确认收货
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate OrderConfirmMethod:waitPayModel];
                        };
                    }
                    
                    if (num == 0) { // 查看物流
                        if (self.delegate && [self.delegate respondsToSelector:@selector(lookGoodsMethod:)]) {
                            [self.delegate lookGoodsMethod:waitPayModel];
                        };
                    }
                    
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
                
            }
            else
            {
                JGroupWaitReceptionCell *cell = [[JGroupWaitReceptionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JGroupWaitReceptionCell class])];

                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.groupModel = _dataGroupArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num,JOrderGroupModel *waitPayModel) {
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                    if (num == 1) { // 确认收货
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate OrderConfirmMethod:waitPayModel];
                        };
                    }
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
            }
        
    }
    
    if (_selOrderType == 2) {
        
        
            if (indexPath.section == 0) {
                JWaitCommentCell *cell = [[JWaitCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JWaitCommentCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.waitPayModel = _dataArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num, JWaitPayModel *waitPayModel) {
                    
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                        [self.delegate OrderCommentMethod:waitPayModel.OrderItemModels[num]];
                    };
                    
                    
                    
                    
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
                
            }
            else
            {
                JGroupCommentCell *cell = [[JGroupCommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JGroupCommentCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.groupModel = _dataGroupArr[indexPath.row];
                });
                
                cell.block = ^(NSInteger num,JOrderGroupModel *waitPayModel) {
                    if (num == 1) { // 评论
                        if (self.delegate && [self.delegate respondsToSelector:@selector(cancleOrderMethod:)]) {
                            [self.delegate OrderCommentMethod:waitPayModel];
                        };
                        
                    }
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
            }
        
    }
    
    if (_selOrderType == 3) {
        
            if (indexPath.section == 0) {
                JReturnProductCell *cell = [[JReturnProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JReturnProductCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.waitPayModel = _dataArr[indexPath.row];
                });
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                cell.block = ^(NSInteger num, JWaitPayDetailModel *waitPayModel) {
                    if (num == 5) { //
                        if (self.delegate && [self.delegate respondsToSelector:@selector(selTypeThireInputProductDetailVC:)]) {
                            [self.delegate selTypeThireInputProductDetailVC:waitPayModel];
                        }
                    }
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                };
                
                return cell;
                
            }
            else
            {
                JGroupReturnCell *cell = [[JGroupReturnCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JGroupReturnCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.groupModel = _dataGroupArr[indexPath.row];
                });
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.block = ^(NSInteger num, JOrderGroupModel *waitPayModel) {
                    
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                };
                
                return cell;
            }
        
    }
    
    if (_selOrderType == 4) {
        
       
            if (indexPath.section == 0) {
            JAccomplishOrderCell *cell = [[JAccomplishOrderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JAccomplishOrderCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.waitPayModel = _dataArr[indexPath.row];
                });

                cell.block = ^(NSInteger num, JWaitPayModel *waitPayModel) {
                    
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                };
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                return cell;
                
            }
            else
            {
                JGroupAccomplishCell *cell = [[JGroupAccomplishCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([JGroupAccomplishCell class])];
                
                
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    
                    cell.groupModel = _dataGroupArr[indexPath.row];
                });
                cell.selectionStyle = UITableViewCellSelectionStyleNone;

                cell.block = ^(NSInteger num, JOrderGroupModel *waitPayModel) {
                    
                    if (num == 10) { // 店铺详情
                        if (self.delegate && [self.delegate respondsToSelector:@selector(goVerdorShopMethod:)]) {
                            [self.delegate goVerdorShopMethod:waitPayModel];
                        }
                    }
                    
                };
                return cell;
            }
        
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (_selOrderType == 0) {
        if (_dataGroupArr.count == 0) {
            JWaitPayModel *payModel = self.dataArr[indexPath.row];
            CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
            return cellHeight;
        }
        else
        {
            if (indexPath.section == 0) {
                JWaitPayModel *payModel = self.dataArr[indexPath.row];
                CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
                return cellHeight;
            }
            else
            {
                CGFloat cellHeight = 45 + 100 + 40 + 50;
                return cellHeight;
            }
        }
        
    }
    if (_selOrderType == 1) {
        if (_dataGroupArr.count == 0) {
            JWaitPayModel *payModel = self.dataArr[indexPath.row];
            
            CGFloat cellHeight = 0.0 ;
            if ([payModel.OrderStatus isEqualToString:@"待收货"]) {
                cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
            }
            else
            {
                cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40;
            }
            
            return cellHeight;
        }
        else
        {
            if (indexPath.section == 0) {
                JWaitPayModel *payModel = self.dataArr[indexPath.row];
                
                CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
                return cellHeight;
            }
            else
            {
                CGFloat cellHeight = 45 + 100 + 40 + 50;
                return cellHeight;
            }
        }
        
    }
    if (_selOrderType == 2) {
        
        if (_dataGroupArr.count == 0) {
            JWaitPayModel *payModel = self.dataArr[indexPath.row];
            
            CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
            return cellHeight;
        }
        else
        {
            if (indexPath.section == 0) {
                JWaitPayModel *payModel = self.dataArr[indexPath.row];
                
                CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 + 50;
                return cellHeight;
            }
            else
            {
                CGFloat cellHeight = 45 + 100 + 40 + 50;
                return cellHeight;
            }
        }
    }
    if (_selOrderType == 3) {
        
        if (_dataGroupArr.count == 0) {
            JWaitPayModel *payModel = self.dataArr[indexPath.row];
            
            CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 ;
            return cellHeight;
        }
        else
        {
            if (indexPath.section == 0) {
                JWaitPayModel *payModel = self.dataArr[indexPath.row];
                
                CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 ;
                return cellHeight;
            }
            else
            {
                CGFloat cellHeight = 45 + 100 + 40 ;
                return cellHeight;
            }
        }

    }
    if (_selOrderType == 4) {
        if (_dataGroupArr.count == 0) {
            JWaitPayModel *payModel = self.dataArr[indexPath.row];
            
            CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 ;
            return cellHeight;
        }
        else
        {
            if (indexPath.section == 0) {
                JWaitPayModel *payModel = self.dataArr[indexPath.row];
                
                CGFloat cellHeight = 45 + payModel.OrderItemModels.count * (100 + 10) + 40 ;
                return cellHeight;
            }
            else
            {
                CGFloat cellHeight = 45 + 100 + 40 ;
                return cellHeight;
            }
        }
    }
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model;
    if (indexPath.section == 0) {
        model = self.dataArr[indexPath.row];
    }
    else
    {
        model = self.dataGroupArr[indexPath.row];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexPath:selOrderType:idModel:)]) {
        [self.delegate selectIndexPath:indexPath selOrderType:_selOrderType  idModel:model];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}





- (void)setSelOrderType:(NSInteger)selOrderType
{
    _selOrderType = selOrderType;
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    [self.tableView reloadData];
}

- (void)setDataGroupArr:(NSArray *)dataGroupArr
{
    _dataGroupArr = dataGroupArr;
    [self.tableView reloadData];
}

@end
